clear all;close all;clc;close all hidden
% Error_all = [3.35 1.44 2.85 1.00 0.33 0.00 2.08
%      56.5 21.2 20.1 30.4 14.3 21.6 11.2];

for CIPIC_0_ARI_1 = 0:1


    Space_all=["Entire" "Front" "Back"];
    cond_all=["bHRTF" "Two-Step" "Monaural L&R"];

    alpha=0.01;
    figure('position',[100 140 600 300])
    subplot(121)

    if CIPIC_0_ARI_1 == 0
        % Horizon = [
        %     32.3 14.7 17.6 % binaural
        %     48.7 18.23 25.5 % 2step
        %     27.9 5.3 6.8% LR
        %     ];
        % Vert = [
        %     36.9 25.6 19.5
        %     58.7 44.7 3.61
        %     31.5 24.3 17.1];
        % Horizon_std = [
        %     45.9 30.3 31.0
        %     50.2 27.3 35.4
        %     43.6 15.1 19.5];
        % Vert_std = [
        %     39.3 24.2 21.0
        %     49.9 36.2 33.2
        %     37.3 23.7 18.3];
        %
        % Horizon = [
        %     28.8 12.8 16.3 % binaural
        %     39.6 17.7 21.1 % 2step
        %     20.7 6.1 7.8 % LR
        %     ];
        % Vert = [
        %     44.4 33.1 32.0
        %     72.2 48.3 43.9
        %     41.3 30.4 27.8];
        % Horizon_std = [
        %     40.2 24.2 28.0
        %     41.5 23.8 27.0
        %     34.3 15.7 19.5];
        % Vert_std = [
        %     44.5 32.0 35.4
        %     51.4 38.1 35.1
        %     44.9 31.8 33.2];
        %
        % Horizon = [
        %     28.2 12.7 17.4 % binaural
        %     35.2 17.0 18.0 % 2step
        %     23.8 6.7 8.90 % LR
        %     ];
        % Vert = [
        %     45.1 38.0 32.2
        %     70.9 52.0 38.9
        %     41.3 33.7 26.2];
        % Horizon_std = [
        %     37.6 21.9 27.1
        %     36.8 21.9 26.1
        %     35.4 15.5 21.1];
        % Vert_std = [
        %     45.2 35.1 35.5
        %     51.0 38.8 31.9
        %     44.5 34.3 32.6];

        Horizon = [
            3.35 1.44 2.85 % binaural
            1.00 0.33 0.00 % 2step
            5.66 2.37 3.32% LR
            ];
        Vert = [
            56.5 21.2 20.1
            30.4 14.3 21.6
            54.56 3.61 8.02];
        Horizon_std = [
            6.05 3.81 5.86
            3.27 1.80 0.00
            8.32 4.96 6.35];
        Vert_std = [
            59.2 30.4 27.4
            50.9 22.6 31.4
            59.76 10.31 13.94];

    elseif CIPIC_0_ARI_1 == 1
        Horizon = [
            19.6 5.50 8.18 % binaural
            26.6 11.8 24.9 % 2step
            20.4 4.64 4.24 % LR
            ];
        Vert = [
            25.0 24.0 10.4
            63.3 41.0 37.9
            23.7 24.7 12.6];
        Horizon_std = [
            32.3 15.6 19.5
            34.0 16.4 25.4
            33.7 13.2 14.5];
        Vert_std = [
            34.3 24.6 12.3
            55.3 32.7 26.4
            32.2 25.1 14.9];
        % Lower_res_hor = [2.08];
        % Lower_res_ver = [11.2];
    end

    % Lower_res_std_H = [];
    % Lower_res_std_V =[];
    % STDS for the last condition:

    shift = 0.23;
    width = 0.21;

    bar((1:3)-shift,Horizon(1,:),width);hold on;
    bar((1:3),Horizon(2,:),width);hold on;
    bar((1:3)+shift,Horizon(3,:),width);hold on;
    errorbar((1:3)-shift,Horizon(1,:),[], Horizon_std(1,:),'k.')
    errorbar((1:3),Horizon(2,:),[], Horizon_std(2,:),'k.')
    errorbar((1:3)+shift,Horizon(3,:),[], Horizon_std(3,:),'k.')
    % text(1:3,Horizon(1,:)+Horizon_std(1,:),'p<0.001')
    title('A. Horizontal Errors', 'fontsize', 12);
    xlabel('Experimental Conditions','fontsize', 12)
    ylabel('Averaged error (Degrees)','fontsize', 12);
    xlim([0 4]);
    if CIPIC_0_ARI_1 == 0
        ylim([0 20])
    else
        ylim([0 70])
    end
    set(gca,'xtick',1:3,'xticklabel',Space_all)
    legend(cond_all);box off;
    legend boxoff



    subplot(122)
    bar((1:3)-shift,Vert(1,:),width);hold on;
    bar((1:3),Vert(2,:),width);hold on;
    bar((1:3)+shift,Vert(3,:),width);hold on;
    errorbar((1:3)-shift,Vert(1,:),[], Vert_std(1,:),'k.')
    errorbar((1:3),Vert(2,:),[], Vert_std(2,:),'k.')
    errorbar((1:3)+shift,Vert(3,:),[], Vert_std(3,:),'k.')
    % text(1:3,Horizon(1,:)+Horizon_std(1,:),'p<0.001')
    title('B. Vertical Errors', 'fontsize', 12);
    xlabel('Experimental Conditions','fontsize', 12)
    ylabel('Averaged error (Degrees)','fontsize', 12);
    xlim([0 4]);

    ylim([0 120])

    set(gca,'xtick',1:3,'xticklabel',Space_all)
    legend(cond_all);box off;
    legend boxoff
    if CIPIC_0_ARI_1 == 0
        exportgraphics(gcf, 'Figure_6_error_results.jpg', 'Resolution', 300,'ContentType', 'vector')
    else
        exportgraphics(gcf, 'Figure_7_error_results_ari.jpg', 'Resolution', 300,'ContentType', 'vector')
    end



    % % whole:
    % Hori_anova = [];Verti_anova=[];Conditions=[];Space=[];
    % for One1_Two2 = 1:3 % 1, binaural HRTFs
    %     for whole0_front1_back2 = 0:2
    %         eval(['load CondAIR_' num2str(One1_Two2) '_WFB_'  num2str(whole0_front1_back2)]);
    %         for kk = 1:length(H_forstd)
    %             Hori_anova=[Hori_anova H_forstd(kk)];
    %             Verti_anova=[Verti_anova V_forstd(kk)];
    %             Conditions=[Conditions cond_all(One1_Two2)];
    %             Space = [Space Space_all(whole0_front1_back2+1)];
    %         end
    %     end
    % end
    %
    % [~,~,stats] = anovan(Hori_anova,{Conditions Space},"Model","interaction", ...
    %     "Varnames",["Condition","Space"]);
    %
    % [~,~,stats] = anovan(Verti_anova,{Conditions Space},"Model","interaction", ...
    %     "Varnames",["Condition","Space"]);

end
